package com.tanhua.management.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.api.MomentsApi;
import com.tanhua.dubbo.server.pojo.Comment;

import com.tanhua.management.vo.CommentVo;
import com.tanhua.management.vo.PageResult;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CommentService {

    @Reference(version = "1.0.0")
    private MomentsApi momentsApi;

    @Autowired
    private UserInfoMapper userInfoMapper;


    /**
     *  评论管理
     *      评论列表翻页
     * @param page  页尺寸
     * @param pageSize 页数
     * @param sortProp 排序字段
     * @param sortOrder ascending 升序 descending 降序
     * @param messageID 用户ID
     * @return
     */
    public PageResult queryCommentList(Integer pageSize, Integer page, String sortProp, String sortOrder,String messageID) {

        // 1. 新建一个PageResult 待用
        PageResult pageResult = new PageResult();
        // 2. 通过圈子api的方法queryCommentListBymessageID(自己写的) 获得评论列表
        List<Comment> commentList =this.momentsApi.queryCommentListByMessageID(pageSize, page, sortProp, sortOrder, messageID);
        // 3. 如果查不到评论列表,直接返回null
        if (CollectionUtils.isEmpty(commentList)) {
            return null;
        }

        // 4. 封装pageResult对象
        pageResult.setPage(page);//当前页码
        pageResult.setPagesize(pageSize);//页大小
        pageResult.setCounts(commentList.size());//总条数
        Integer pageTotalNum = commentList.size() % pageResult.getPagesize() == 0 ? commentList.size() / pageResult.getPagesize() : commentList.size() / pageResult.getPagesize() + 1;
        pageResult.setPages(pageTotalNum);//总页数

        // 5. 查询个人信息
        Set<Object> userIds = commentList.stream().map(Comment::getUserId).collect(Collectors.toSet());
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserInfo::getUserId, userIds);
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);


        // 6. new一个集合接收返回前端的vo
        List<CommentVo> commentVoList = new ArrayList<>();
        // 7. 遍历评论列表,给返回前端的CommentVo的属性赋值
        for (Comment record : commentList) {
            CommentVo commentVo = new CommentVo();
            commentVo.setContent(record.getContent());// 评论内容
            commentVo.setId(record.getId().toHexString()); // 编号
            commentVo.setUserId(Integer.valueOf(record.getUserId() + "")); // 评论人ID

            Integer date = Integer.valueOf(String.valueOf(new SimpleDateFormat("yyyyMMdd").format(record.getCreated())));
            commentVo.setCreateDate(date);// 评论时间
            // 7.1 遍历个人信息列表集合
            for (UserInfo userInfo : userInfoList) {
                // 设置昵称
                if(record.getUserId().longValue() == userInfo.getUserId().longValue()) {
                    commentVo.setNickname(userInfo.getNickName());
                    commentVoList.add(commentVo);
                }
            }


        }



        pageResult.setItems(commentVoList);
        return pageResult;

    }


}
